import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

import edu.cmu.ece.ds.droidchat.message.DroidChatMessage;


/**
 * For each logged in user, the server creates a Login_Session_Attr
 *
 */
public class Login_Session_Attr {
	private String username;
	private int login_session_id;
	private BigInteger modulus;
	private BigInteger exponent;
	private String IP;
	
	private int round = 0;
	private double last_known_lattitude;
	private double last_known_longitude;
	private long lastUpdateTime;  // the latest location update time
								  //  server removes user after LOCATION_TIMEOUT

	public static int LOCATION_TIMEOUT = 80000;
	
	public Login_Session_Attr(int login_session_id, String username)
	{
		this.login_session_id=login_session_id;
		this.username=username;
	}
	
	public int getLogin_session_id(){		return login_session_id;}
	public String getUsername() 	{		return username;		}
	public BigInteger getModulus() 	{		return modulus;			}
	
	public void setModulus(BigInteger modulus) {
		this.modulus = modulus;
	}
	public BigInteger getExponent() {
		return exponent;
	}
	public void setExponent(BigInteger exponent) {
		this.exponent = exponent;
	}
	
	public double getLast_known_lattitude() {	return last_known_lattitude;	}
	public void setLast_known_lattitude(double lattitude) {
		last_known_lattitude = lattitude;	
	}
	public double getLast_known_longitude() {	return last_known_longitude;		}
	public void setLast_known_longitude(double longitude) {
		last_known_longitude = longitude;
	}
	public int getRound() {		return round;	}
	public void setRound(int round) {	this.round = round;	}
	
	public String getIP() {		return IP;			}
	public void setIP(String iP) {		IP = iP;	}
	
	public long getLastUpdateTime() {return lastUpdateTime;	}
	public void setLastUpdateTime(long lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}
	
	public BigInteger encrypt(Object sendmes)
	{
		try {
			BigInteger encrint;
			ByteArrayOutputStream bo = new ByteArrayOutputStream();
			ObjectOutputStream oo = new ObjectOutputStream(bo);
	        oo.writeObject(sendmes);
			byte[] plaintext=bo.toByteArray();
			BigInteger plainint=new BigInteger(1,plaintext);
			encrint=plainint.modPow(exponent,modulus);
			System.out.println(plainint);
			return encrint;
//			System.out.println(encrint);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public Object decrypt(BigInteger encrmes)
	{

		try {
			BigInteger encrint=encrmes;
			BigInteger plainint=encrint.modPow(exponent,modulus);
//			System.out.println(plainint);
//			System.out.println(encrint);
			byte[] plaintext1=plainint.toByteArray();
			byte[] plaintext=plaintext1;
			if(plaintext1[0]==0)
				plaintext=new byte[plaintext1.length-1];
			System.arraycopy(plaintext1, 1, plaintext, 0, plaintext.length);
	        ByteArrayInputStream bi = new ByteArrayInputStream(plaintext);
	        ObjectInputStream oi = new ObjectInputStream(bi);
	        Object receivemes=(Object) oi.readObject();
//			System.out.println((Message)receivemes);
	        return receivemes;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String toString(){
		return "Login Attribute: exponent=" + this.exponent + " modulus=" + this.modulus
		+ " username=" + this.username + " loginsessionid=" + this.login_session_id;
	}	
}
